home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’96 / VideoFolder 1.0a / Source / MoreFiles 1.4.1 / FullPath.c < prev    next >
Text File  |  1995-12-21  |  7KB  |  210 lines

  1. /*
  2. **    Apple Macintosh Developer Technical Support
  3. **
  4. **    Routines for dealing with full pathnames... if you really must.
  5. **
  6. **    by Jim Luther, Apple Developer Technical Support Emeritus
  7. **
  8. **    File:        FullPath.c
  9. **
  10. **    Copyright © 1995 Apple Computer, Inc.
  11. **    All rights reserved.
  12. **
  13. **    You may incorporate this sample code into your applications without
  14. **    restriction, though the sample code has been provided "AS IS" and the
  15. **    responsibility for its operation is 100% yours.  However, what you are
  16. **    not permitted to do is to redistribute the source as "DSC Sample Code"
  17. **    after having made changes. If you're going to re-distribute the source,
  18. **    we require that you make it clear in the source that the code was
  19. **    descended from Apple Sample Code, but that you've made changes.
  20. */
  21.  
  22. #include <Types.h>
  23. #include <Errors.h>
  24. #include <Memory.h>
  25. #include <Files.h>
  26. #include <TextUtils.h>
  27. #include <Aliases.h>
  28. #include "FSpCompat.h"
  29. #include "FullPath.h"
  30.  
  31. /*
  32.     IMPORTANT NOTE:
  33.     
  34.     The use of full pathnames is strongly discouraged. Full pathnames are
  35.     particularly unreliable as a means of identifying files, directories
  36.     or volumes within your application, for two primary reasons:
  37.     
  38.     •     The user can change the name of any element in the path at virtually
  39.         any time.
  40.     •    Volume names on the Macintosh are *not* unique. Multiple
  41.         mounted volumes can have the same name. For this reason, the use of
  42.         a full pathname to identify a specific volume may not produce the
  43.         results you expect. If more than one volume has the same name and
  44.         a full pathname is used, the File Manager currently uses the first
  45.         mounted volume it finds with a matching name in the volume queue.
  46.     
  47.     In general, you should use a file’s name, parent directory ID, and
  48.     volume reference number to identify a file you want to open, delete,
  49.     or otherwise manipulate.
  50.     
  51.     If you need to remember the location of a particular file across
  52.     subsequent system boots, use the Alias Manager to create an alias record
  53.     describing the file. If the Alias Manager is not available, you can save
  54.     the file’s name, its parent directory ID, and the name of the volume on
  55.     which it’s located. Although none of these methods is foolproof, they are
  56.     much more reliable than using full pathnames to identify files.
  57.     
  58.     Nonetheless, it is sometimes useful to display a file’s full pathname to
  59.     the user. For example, a backup utility might display a list of full
  60.     pathnames of files as it copies them onto the backup medium. Or, a
  61.     utility might want to display a dialog box showing the full pathname of
  62.     a file when it needs the user’s confirmation to delete the file. No
  63.     matter how unreliable full pathnames may be from a file-specification
  64.     viewpoint, users understand them more readily than volume reference
  65.     numbers or directory IDs.
  66.     
  67.     The following technique for constructing the full pathname of a file is
  68.     intended for display purposes only. Applications that depend on any
  69.     particular structure of a full pathname are likely to fail on alternate
  70.     foreign file systems or under future system software versions.
  71. */
  72.  
  73. /*****************************************************************************/
  74.  
  75. pascal    OSErr    GetFullPath(short vRefNum,
  76.                             long dirID,
  77.                             StringPtr name,
  78.                             short *fullPathLength,
  79.                             Handle *fullPath)
  80. {
  81.     OSErr        result;
  82.     FSSpec        spec;
  83.     
  84.     result = FSMakeFSSpecCompat(vRefNum, dirID, name, &spec);
  85.     if ( result == noErr )
  86.     {
  87.         result = FSpGetFullPath(&spec, fullPathLength, fullPath);
  88.     }
  89.     
  90.     return ( result );
  91. }
  92.  
  93. /*****************************************************************************/
  94.  
  95. pascal    OSErr    FSpGetFullPath(const FSSpec *spec,
  96.                                short *fullPathLength,
  97.                                Handle *fullPath)
  98. {
  99.     OSErr        result;
  100.     FSSpec        tempSpec;
  101.     CInfoPBRec    pb;
  102.     
  103.     /* Make a copy of the input FSSpec that can be modified */
  104.     BlockMoveData(spec, &tempSpec, sizeof(FSSpec));
  105.     
  106.     if ( tempSpec.parID == fsRtParID )
  107.     {
  108.         /* The object is a volume */
  109.         
  110.         /* Add a colon to make it a full pathname */
  111.         ++tempSpec.name[0];
  112.         tempSpec.name[tempSpec.name[0]] = ':';
  113.         
  114.         /* We're done */
  115.         result = PtrToHand(&tempSpec.name[1], fullPath, tempSpec.name[0]);
  116.     }
  117.     else
  118.     {
  119.         /* The object isn't a volume */
  120.         
  121.         /* Put the object name in first */
  122.         result = PtrToHand(&tempSpec.name[1], fullPath, tempSpec.name[0]);
  123.         if ( result == noErr )
  124.         {
  125.             /* Get the ancestor directory names */
  126.             pb.dirInfo.ioNamePtr = tempSpec.name;
  127.             pb.dirInfo.ioVRefNum = tempSpec.vRefNum;
  128.             pb.dirInfo.ioDrParID = tempSpec.parID;
  129.             do    /* loop until we have an error or find the root directory */
  130.             {
  131.                 pb.dirInfo.ioFDirIndex = -1;
  132.                 pb.dirInfo.ioDrDirID = pb.dirInfo.ioDrParID;
  133.                 result = PBGetCatInfoSync(&pb);
  134.                 if ( result == noErr )
  135.                 {
  136.                     /* Append colon to directory name */
  137.                     ++tempSpec.name[0];
  138.                     tempSpec.name[tempSpec.name[0]] = ':';
  139.                     
  140.                     /* Add directory name to beginning of fullPath */
  141.                     (void) Munger(*fullPath, 0, NULL, 0, &tempSpec.name[1], tempSpec.name[0]);
  142.                     result = MemError();
  143.                 }
  144.             } while ( (result == noErr) && (pb.dirInfo.ioDrDirID != fsRtDirID) );
  145.         }
  146.     }
  147.     if ( result == noErr )
  148.     {
  149.         /* Return the length */
  150.         *fullPathLength = GetHandleSize(*fullPath);
  151.     }
  152.     else
  153.     {
  154.         /* Dispose of the handle and return NULL and zero length */
  155.         DisposeHandle(*fullPath);
  156.         *fullPath = NULL;
  157.         *fullPathLength = 0;
  158.     }
  159.     
  160.     return ( result );
  161. }
  162.  
  163. /*****************************************************************************/
  164.  
  165. pascal OSErr FSpLocationFromFullPath(short fullPathLength,
  166.                                      const void *fullPath,
  167.                                      FSSpec *spec)
  168. {
  169.     AliasHandle    alias;
  170.     OSErr        result;
  171.     Boolean        wasChanged;
  172.     Str32        nullString;
  173.     
  174.     /* Create a minimal alias from the full pathname */
  175.     nullString[0] = 0;    /* null string to indicate no zone or server name */
  176.     result = NewAliasMinimalFromFullPath(fullPathLength, fullPath, nullString, nullString, &alias);
  177.     if ( result == noErr )
  178.     {
  179.         /* Let the Alias Manager resolve the alias. */
  180.         result = ResolveAlias(NULL, alias, spec, &wasChanged);
  181.         
  182.         DisposeHandle((Handle)alias);    /* Free up memory used */
  183.     }
  184.     return ( result );
  185. }
  186.  
  187. /*****************************************************************************/
  188.  
  189. pascal OSErr LocationFromFullPath(short fullPathLength,
  190.                                   const void *fullPath,
  191.                                   short *vRefNum,
  192.                                   long *parID,
  193.                                   Str31 name)
  194. {
  195.     OSErr    result;
  196.     FSSpec    spec;
  197.     
  198.     result = FSpLocationFromFullPath(fullPathLength, fullPath, &spec);
  199.     if ( result == noErr )
  200.     {
  201.         *vRefNum = spec.vRefNum;
  202.         *parID = spec.parID;
  203.         BlockMoveData(&spec.name[0], &name[0], spec.name[0] + 1);
  204.     }
  205.     return ( result );
  206. }
  207.  
  208. /*****************************************************************************/
  209.  
  210.